package cn.heapstack.jpa.main;

import java.util.UUID;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import cn.heapstack.jpa.domain.Address;
import cn.heapstack.jpa.domain.Customer;
import cn.heapstack.jpa.domain.Customer2;
import cn.heapstack.jpa.domain.CustomerPK;
import cn.heapstack.jpa.domain.CustomerType;

public class Main {

	/**
	 * 这个工程的例子生成三张数据库的表
	 * MPO_Customer_Using_EmbeddedID
	 * MPO_Customer_Using_IdClass
	 * MPO_ADDRESS_TABLE
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		EntityManagerFactory factory = Persistence
			.createEntityManagerFactory("MappingPersistentObject");
		EntityManager em = factory.createEntityManager();

		em.getTransaction().begin();
		
		String lastName = UUID.randomUUID().toString();
		long ssn = 100000l;
		
		CustomerPK pk = new CustomerPK();
		pk.setSsn(ssn);
		pk.setLastName(lastName);
		
		Customer cust = new Customer();
		cust.setSsn(ssn);
		cust.setLastName(lastName);
		cust.setFirstName("c1_firstName");
		cust.setCity("city");
		cust.setState("state");
		cust.setStreet("street");
		
		em.persist(cust);
		
		Customer find = em.find(Customer.class, pk);
		System.out.println("Customer 1 : " + find.getFirstName() );

		
		CustomerPK pk2 = new CustomerPK();
		pk2.setSsn(ssn);
		pk2.setLastName(lastName);
		Customer2 c2 = new Customer2();
		c2.setPk(pk2);
		c2.setFirstName("c2_firstName");
		c2.setCustomerType(CustomerType.REGISTERED);
		Address address = new Address(); 
		address.setCity("city");
		address.setState("state");
		address.setStreet("street");
		c2.setAddress(address);
		
		em.persist(c2);
		
		Customer2 find2 = em.find(Customer2.class, pk2);
		System.out.println("Customer 2 : " + find2.getFirstName() );
		System.out.println("Customer 2's customerType : "+ find2.getCustomerType() );
		
		em.getTransaction().commit();
		em.close();
		factory.close();
	}

}
